package com.wlyuan.index.remote.facade;

import com.alibaba.fastjson.JSON;
import com.wlyuan.core.domain.valueobj.CombineId;
import com.wlyuan.core.domain.valueobj.TenantId;
import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.index.api.dto.combine.CombineIndexDTO;
import com.wlyuan.index.api.dto.combine.CombineReindexAllDTO;
import com.wlyuan.index.api.dto.combine.CombineReindexDTO;
import com.wlyuan.index.domain.source.adapter.CombineAdapter;
import com.wlyuan.index.infrastructure.exception.CombineNoFound;
import com.wlyuan.tools.Sort;
import com.wlyuan.transport.api.ICombineQueryService;
import com.wlyuan.transport.dto.query.CombineDataFilterDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuqingqing
 * <p>
 * 合单防腐层
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CombineFacade {

    @DubboReference(check = false)
    private ICombineQueryService combineQueryService;

    public CombineIndexDTO getCombineById(CombineId combineId) {
        return this.getCombineById(combineId.getId());
    }

    @Deprecated
    private CombineIndexDTO getCombineById(Long combineId) {
        try {
            var combine = combineQueryService.getCombineById(combineId);
            AssertUtils.assertNotNull(combine, new CombineNoFound(), combineId);
            if (logger.isDebugEnabled()) {
                logger.debug("Combine: {}", JSON.toJSONString(combine));
            }
            return CombineAdapter.translate(combine);
        } catch (Exception e) {
            logger.error("Query combine error: {}", combineId, e);
        }
        return null;
    }

    public List<CombineId> listIds(TenantId tenantId, long page, long size) {
        var reindex = new CombineReindexDTO();
        reindex.setTenantId(tenantId.getId());
        return this.listIds(reindex, page, size);
    }

    public List<CombineId> listIds(long page, long size) {
        var reindex = new CombineReindexDTO();
        return this.listIds(reindex, page, size);
    }

    public List<CombineId> listIds(CombineReindexAllDTO reindexAll, long page, long size) {
        CombineReindexDTO reindex = new CombineReindexDTO();
        BeanUtils.copyProperties(reindexAll, reindex);
        return this.listIds(reindex, page, size);
    }

    public List<CombineId> listIds(CombineReindexDTO reindex, long page, long size) {
        var filter = new CombineDataFilterDTO();
        filter.setTenantId(reindex.getTenantId());
        filter.setCreateTimeBegin(reindex.getCreateAtBegin());
        filter.setCreateTimeEnd(reindex.getCreateAtEnd());
        filter.setSize(size);
        filter.setPage(page);
        var sorts = new ArrayList<Sort>();
        var sort = new Sort();
        sort.setAsc(false);
        sort.setName("id");
        sorts.add(sort);
        filter.setSort(sorts);
        return combineQueryService.listIds(filter)
                .stream()
                .map(CombineId::new)
                .collect(Collectors.toList());
    }
}

